<?php

	#doc
	#	classname:	QAfishaController
	#	scope:		PUBLIC
	#
	#/doc

	class QafishaController extends Controller
	{
	
	
		public $img;
		public $layout = "admin";
		
		public function filters()
		{
			return array(
				'accessControl',
			);
		}

		public function accessRules()
		{
		return array(


		array('allow',
			'actions'=>array('AddFilm','Add','Delete','Save','AddTiming','List','Del','Edit'),
			'roles'=>array('admin')

			),
			array('deny',
			'actions'=>array('AddFilm','Add','Delete','Save','AddTiming','List','Del','Edit'),
			'users'=>array('*')

			),


		);
	 }
		
		public function actionAddFilm()
		{
			//$type=event_tag::model()->findAll();
				$place=thinema_theatre::model()->findAll();
				$type=event_tag::model()->findAll();		
			$this->render('application.views.qafisha.addafishafilm', array("place"=>$place,"type"=>$type));
		}	
		
		
		public function actionAdd()
		{
			$type=event_tag::model()->findAll();
				

			$this->render('application.views.qafisha.addafisha', array("type"=>$type));
		}
		
		public function actionSave()
		{
			if((isset($_POST['title']))&&(isset($_POST['area2'])))
			{
				$events = new events();
				$events->name=$_POST['title'];
				$events->text=$_POST['area2'];
				$events->producer=$_POST['producer'];
				$events->director=$_POST['director'];
				$events->inection=$_POST['inection'];
				$events->ganer=$_POST['ganer'];
				// Получаем дату начала события
				$date_start=$_POST['date_Y']."-".$_POST['date_M']."-".$_POST['date_D'];
					$events->start=$date_start;
				//Получаем дату конца события
				$date_end=$_POST['date_Y_end']."-".$_POST['date_M_end']."-".$_POST['date_D_end'];
				$events->end=$date_end;
							
				//Загружаем картинку
				$img = CUploadedFile::getInstance($events,'image');
				$upload = Yii::getPathOfAlias('open29').Yii::app()->controller->module->uploadPath;  


				if($img != null)
				{      
					$img->saveAs("images/events/".$img);   
				}
				$events->image=$img;
				
				if($events->save())
				{	
					$criteria=new CDbCriteria;
					$criteria->order='id_event DESC';
					$criteria->limit='1';
						$id_event=events::model()->find($criteria);
						$id=$id_event->id_event;
						$type=$_POST['type'];
						foreach($type  as $key)
						{
							$link_tag= new link_tag();
							$link_tag->tag_name=$key;
							$link_tag->id_event=$id;
							$link_tag->save();
						}
						
					$this->redirect(array('list'));
				}
				else
				{
					
				}
			}
		}
		
		
		
		
		public function actionAddTiming ()
		{			

			$event_id=$_GET['id'];
			$event=events::model()->findByPk($event_id);
				$start=$event['start'];
					$end=$event['end'];
				  $dt=Array($start,$end);
						$dates=Array();
							$i=0;
					while (strtotime($dt[1])>=strtotime("+".$i." day",strtotime($dt[0])))
					{
						$dates[]=date("Y-m-d",strtotime("+".$i++." day",strtotime($dt[0])));
					}
			$theatre=thinema_theatre::model()->findAll();
			$this->render("application.views.qafisha.addTiming",array('theatre'=>$theatre,"dates"=>$dates,'id_event'=>$event_id));
		}

		
		public function actionList()
		{
			$events=events::model()->findAll();
			$this->render('application.views.qafisha.list',array("events"=>$events));
		}
		
		
		public function actionDel()
		{
			$id_event=$_GET['id'];
			//$events=new events();
			
			events::model()->deleteAll("id_event=:id_event",array("id_event"=>$id_event));			
			link_tag::model()->deleteAll("id_event=:id_event",array("id_event"=>$id_event));
			event_time::model()->deleteAll("id_event=:id_event",array("id_event"=>$id_event));
			$this->redirect('/adminka/qafisha/list');
		}
		
		
		public function actionEdit()
		{
			$id_event=$_GET['id'];
			$type=event_tag::model()->findAll();
			$event=events::model()->findByPk($id_event);
			$this->render('application.views.qafisha.update',array('type'=>$type,'event'=>$event));	
		}

		
		public function actionUpdate ()
			{				
				if ((isset($_POST['id']))&&(isset($_POST['title']))&&(isset($_POST['area2']))&&(isset($_POST['type'])))
				{
					$events=events::model()->findByPk($_POST['id']);
					$events->name=$_POST['title'];
					$events->text=$_POST['area2'];
					// Получаем дату начала события
					$date_start=$_POST['date_Y']."-".$_POST['date_M']."-".$_POST['date_D'];
					$events->start=$date_start;
					//Получаем дату конца события
					$date_end=$_POST['date_Y_end']."-".$_POST['date_M_end']."-".$_POST['date_D_end'];
					$events->end=$date_end;
					//Загружаем картинку
					$img = CUploadedFile::getInstance($events,'image');
					$upload = Yii::getPathOfAlias('open29').Yii::app()->controller->module->uploadPath;
					if($img != null)
					{      
						$img->saveAs("images/events/".$img);   
					}
					$events->image=$img;
					//Обновляем запись
					if ( $events->save() )
					{
						$this->redirect(array('list'));
					}
					else{}
				}

			}

		
		public function actionSaveTiming ()
		{
			$theatre=$_POST['theatre'];
				$date=$_POST['date'];
					$id_event=$_POST['id_event'];
			if((isset($theatre))&&(isset($date)))
			{
			$event_time= new event_time();
				$event_time->id_event=$id_event;
					$event_time->date=$date;
						$event_time->place=$theatre;
							$event_time->save();

			}

			$criteria=new CDbCriteria;
				$criteria->order='id_time DESC';
				$criteria->limit='1';

			$id_time=event_time::model()->find($criteria);

			$timing_h=$_POST['timing_h'];
			$timing_m=$_POST['timing_m'];
			$hall=$_POST['hall'];
			$price=$_POST['price'];



			for ( $i=0; $i < count($timing_h); $i++ )
			{ 
				if($timing_h[$i]!="")
				{
				$film_timing = new film_timing();
					$film_timing->time=$timing_h[$i].":".$timing_m[$i].":"."00";
					$film_timing->id_time=$id_time->id_time;
					$film_timing->price=$price[$i];
					$film_timing->hall=$hall[$i];
					$film_timing->save();
				}
			}
			$this->redirect('/adminka/qafisha/list');
		}
		

	}
	###

?>